#ifndef REC_TANGLE
#define REC_TANGLE

#include "../math/math_operation.hpp"
#include "plane.hpp"

class Rectangle : public Plane {

public:
  /**
   * @brief  Default Constructor
   * @note
   * @retval
   */
  Rectangle();

  /**
   * @brief  Use normal to specify the rectangle normal, and use o and size
   * specify the position and bounds of the rectangle
   * @note
   * @param  n: the normal of the rectangle
   * @param  o: the geometry center of the rectangle
   * @param  s: the bounds of the rectangle
   * @retval
   */
  Rectangle( vec3f n, vec3f o, vec2f s );

  vec3f MinVec() const { return min; }

  vec3f MaxVec() const { return max; }

  vec3f Center() const { return origin(); }

  const vec2f &Size() const { return size; }

  void Size( const vec2f &s ) {
    size = s;
    CacullateMinMax();
  }

  /**
   * @brief  Check if a point is in the the rectangle
   * @note
   * @param  point: the checked point
   * @retval true for inside else for missing
   */
  bool Contains( const vec3f &point );


    /**
   * @brief  Check if a point is in the the rectangle(use general method)
   * @note
   * @param  o: the origin point
   * @param  p: the checked point
   * @retval true for inside else for missing
   */
  bool Contains(const vec3f & o, const vec3f & p);
private:
  vec2f size;
  vec3f min;
  vec3f max;

  void CacullateMinMax() {
    vec3f up = normal();
    vec3f forward = normalize( origin() );
    vec3f right = normalize( cross( forward, up ) );
    vec2f half = size / 2.f;
    vec3f offsetX = half.x * forward;
    vec3f offsetY = half.y * right;
    min = origin() - offsetX - offsetY;
    max = origin() + offsetX + offsetY;
  }

  SHAPE_HIT_SIGNITURE
};

#endif